package com.wedo.user.controller;

import com.wedo.auth.pojo.UserInfo;
import com.wedo.auth.utils.JwtUtils;
import com.wedo.common.pojo.PageResult;
import com.wedo.user.config.JwtProperties;
import com.wedo.user.pojo.Review;
import com.wedo.user.service.ReviewService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("review")
public class ReviewController {

    @Autowired
    private ReviewService reviewService;

    @Autowired
    private JwtProperties jwtProperties;

    @GetMapping("page")
    public ResponseEntity<PageResult<Review>> queryReviewByBidAndPage(
            @RequestParam(value = "bid") Long bid,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "6") Integer rows
    ) {
        PageResult<Review> result = this.reviewService.queryReviewByBidAndPage(bid,page,rows);
        if (result == null || CollectionUtils.isEmpty(result.getItems())) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping
    public ResponseEntity<Boolean> addReview(
            @RequestHeader("WD_TOKEN") String token,
            @RequestBody Review review
    ) {
        if (review.getbId() == null || StringUtils.isBlank(review.getText())) {
            return ResponseEntity.badRequest().build();
        }
        UserInfo userinfo = null;
        // 解析jwt
        try {
            userinfo = JwtUtils.getInfoFromToken(token, this.jwtProperties.getPublicKey());
            if ("ADMIN".equals(userinfo.getRole())) {
                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
            }
            review.setuId(userinfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
        Boolean result = this.reviewService.addReview(review);
        return ResponseEntity.ok(result);
    }

    @DeleteMapping
    public ResponseEntity<Boolean> deleteReview(
            @RequestHeader("WD_TOKEN") String token,
            @RequestBody Review review)
    {
        if (review.getId() == null) {
            return ResponseEntity.badRequest().build();
        }
        UserInfo userinfo = null;
        // 解析jwt
        try {
            userinfo = JwtUtils.getInfoFromToken(token, this.jwtProperties.getPublicKey());
            if ("ADMIN".equals(userinfo.getRole())) {
                Boolean result = this.reviewService.adminDeleteReview(review);
                return ResponseEntity.ok(result);
            }
            review.setuId(userinfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
        Boolean result = this.reviewService.deleteReview(review);
        return ResponseEntity.ok(result);
    }

}
